package com.hxkj.client.service.taglib.directive;

import com.hxkj.client.service.ColumnServiceClient;
import com.hxkj.client.service.ManuscriptServiceClient;
import com.hxkj.domain.context.EnvironmentContextHolder;
import com.hxkj.domain.po.Column;
import com.hxkj.domain.po.Manuscript;
import com.hxkj.domain.po.WebsiteAddr;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @ClassName 面包屑自定义标签
 * @Description
 * @Author chenfangzhou
 * @Date 2018/7/18 16:14
 * @Version 1.0
 */
@Component
@Slf4j
public class BreadcrumbsDirective extends AbstractDirective {
    @Autowired
    private ManuscriptServiceClient manuscriptServiceClient;

    @Autowired
    private ColumnServiceClient columnServiceClient;


    // 文章分类id
    private static final String COLUMN_ID = "columnId";

    // 站点id
    private static final String SITE_ID = "siteId";

    // 面包屑类型 1文章详情和列表  2单页
    private static final String BREADCRUMBS_TYPE = "breadcrumbsType";

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        try {
            Long siteId = getLong(SITE_ID, params) == null ? (Long) getCustomAttrbute(env, SITE_ID) : getLong(SITE_ID, params);
            Integer breadcrumbsType = getInt(BREADCRUMBS_TYPE, params) == null ? 2 : getInt(BREADCRUMBS_TYPE, params);
            //文章详情和文章列表
            List<Column> navColumnList = new ArrayList<>();
            String note = super.getNote(env);
            if (1 == breadcrumbsType) {
                Long columnId = getLong(COLUMN_ID, params) == null ? (Long) getCustomAttrbute(env, COLUMN_ID) : getLong(COLUMN_ID, params);
                if (columnId == null) {
                    log.debug("文章分类id不能为空!");
                }
                Column column1 = new Column();
                column1.setColumnId(columnId);
                column1.setSiteId(siteId);

                // 不包含首页,并且顺序的反的
                navColumnList = columnServiceClient.getTopParentColumn(column1);
                WebsiteAddr webSiteAddr = getWebSiteAddr(siteId);
                if (StringUtils.isNotBlank(note)) {
                    for (Column column : navColumnList) {
                        column.setManuscriptPath(webSiteAddr.getIpAdd() + column.getManuscriptPath());
                    }
                }

                // 添加首页
                Column column = this.getIndexUrl(siteId, env);
                if (column == null) {
                    column = new Column();
                }
                if (StringUtils.isNotBlank(note)) {
                    column.setManuscriptPath(webSiteAddr.getIpAdd() + column.getManuscriptPath());
                }else{

                }
                navColumnList.add(column);
                Collections.reverse(navColumnList);
            } else if (2 == breadcrumbsType) {
                // 单页
                Column column = this.getIndexUrl(siteId, env);
                if (column == null) {
                    column = new Column();
                }
                navColumnList.add(column);
            }

            //如果是静态页生成,根据情况在url上拼接站点上下文前缀目录
            if (StringUtils.isBlank(note)){
                Map<String, Object> enviroment = EnvironmentContextHolder.getEnvironment();
                Map<Long, String> siteCtxMap = (Map<Long, String>) enviroment.get("siteCtxMap");
                String urlContext = siteCtxMap.get(siteId);
                if(StringUtils.isNotBlank(urlContext)) {
                    for (Column item : navColumnList) {
                        item.setManuscriptPath(urlContext + item.getManuscriptPath());
                    }
                }
            }
            env.setVariable("NavColumnList", DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(navColumnList));
            if (body != null) {
                body.render(env.getOut());
            } else {
                log.debug("面包屑所在模板至少要加一个空格");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Column getIndexUrl(Long siteId, Environment env) throws Exception {
        Manuscript manuscript = new Manuscript();
        manuscript.setSiteId(siteId);
        // 首页频道的别名
        manuscript.setAlisName("index");
        // 类型 1频道 2文章详情 3文章分类 4单页 5聚合
        manuscript.setStatus(1L);
        ResultDataStruct<Manuscript> resultStruct = manuscriptServiceClient.getManuscript(manuscript);
        if(resultStruct.getCode() != 200 ){
            throw new CustomMessageException(resultStruct.getCode(), resultStruct.getMessage());
        }
        Manuscript data = resultStruct.getData();
        if(data == null){
            throw new CustomMessageException(400, "获取首页地址失败");
        }
        Column column = new Column();
        column.setColumnName("首页");
        column.setManuscriptPath(data.getUrlPath());
        return column;
    }
}
